﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TagFilter.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="Hisaab.Data.Filters.TagFilter" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Data.Filters
{
    using System;
    using System.Data.Objects;
    using System.Linq;
    using Entities;

    /// <summary>
    /// Defines the <seealso cref="Hisaab.Data.Filters.TagFilter"/> type.
    /// </summary>
    public static class TagFilter
    {
        /// <summary>
        /// Filters the specified Tag tags.
        /// </summary>
        /// <param name="collection">The Tag tags.</param>
        /// <param name="objectToMatch">The Tag to match.</param>
        /// <returns>List of filtered Tags</returns>
        public static IQueryable<Tag> Filter(this IObjectSet<Tag> collection, Tag objectToMatch)
        {
            return objectToMatch == null ?
                collection :
                from tag in collection
                where (objectToMatch.Id == 0 || tag.Id == objectToMatch.Id) &&
                (objectToMatch.Name == null || tag.Name.Equals(objectToMatch.Name, StringComparison.InvariantCultureIgnoreCase))
                select tag;
        }

        /// <summary>
        /// Filters the by transaction.
        /// </summary>
        /// <param name="tags">The tags list</param>
        /// <param name="transactionId">The transaction id.</param>
        /// <returns>List of tags associated with given transaction id</returns>
        public static IQueryable<Tag> FilterByTransaction(this IObjectSet<Tag> tags, int transactionId)
        {
            return tags.Where(t => t.Transactions.Any(tr => tr.Id == transactionId));
        }
    }
}